package com.hand.sxt25527.ssmtrain.controller;

import com.github.pagehelper.PageInfo;
import com.hand.sxt25527.ssmtrain.dto.ItemEditDto;
import com.hand.sxt25527.ssmtrain.dto.ItemInsertDto;
import com.hand.sxt25527.ssmtrain.dto.ItemQueryCondition;
import com.hand.sxt25527.ssmtrain.entity.Item;
import com.hand.sxt25527.ssmtrain.entity.Msg;
import com.hand.sxt25527.ssmtrain.service.IItemServiceDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Mr~S
 * @date 2019/7/19 11:34
 */
@Controller
@RequestMapping("/item")
@Api(value = "ItemController")
@CrossOrigin
public class ItemController {
    @Autowired
    IItemServiceDao itemServiceDao;


    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("分页查询所有物料信息")
    public Msg findAll(@RequestParam("currentPage") Integer page,@RequestParam("pageSize") Integer size) {

        List<Item> itemList = itemServiceDao.findAll(page, size);
        PageInfo pageInfo = new PageInfo(itemList, 3);
        return Msg.success().add("pageInfo", pageInfo);
    }

    @RequestMapping(value = "/findAllByCondition", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("分页查询所有物料信息")
    public Msg findAllByCondition(@RequestParam("currentPage") Integer page,@RequestParam("pageSize") Integer size, ItemQueryCondition condition) {

        List<Item> itemList = itemServiceDao.findAllByCondition(page, size, condition);
        PageInfo pageInfo = new PageInfo(itemList, 3);
        return Msg.success().add("pageInfo", pageInfo);
    }

    @ApiOperation("新增物料信息")
    @RequestMapping(value = "/insertItem", method = RequestMethod.POST)
    @ResponseBody
    public Msg insertItem(@RequestBody ItemInsertDto itemInsertDto) {
        String itemUom = itemInsertDto.getItemUom();
        String itemDesc = itemInsertDto.getItemDesc();

        if (StringUtils.isEmpty(itemUom) && StringUtils.isEmpty(itemDesc)) {
            return Msg.fail();
        }
        Item item = new Item();
        item.setItemUom(itemUom);
        item.setItemDesc(itemDesc);
        item.setStartActiveDate(itemInsertDto.getStartActiveDate());
        item.setEndActiveDate(itemInsertDto.getEndActiveDate());
        item.setEnableFlag(itemInsertDto.getEnableFlag());
        int count = itemServiceDao.insertItem(item);
        if (count == 0) {
            return Msg.fail();
        }
        return Msg.success();
    }

    @ApiOperation("修改物料信息")
    @RequestMapping(value = "/updateItem", method = RequestMethod.POST)
    @ResponseBody
    public String updateItem(@RequestBody ItemEditDto editDto) {

        Long dtoItemId = editDto.getItemId();
        Item item = itemServiceDao.selectItemById(dtoItemId);
        Long versionNumber = item.getObjectVersionNumber();
        editDto.setObjectVersionNumber(versionNumber);

        itemServiceDao.updateItem(editDto);
        return "success";
    }

    @ApiOperation("删除物料信息")
    /**
     * 使用delete会产生bad request。具体咱也不清楚
     */
    @RequestMapping(value = "/deleteItemById", method = RequestMethod.POST)
    @ResponseBody
    public Msg deleteItemById(@RequestBody Map<String, String> itemId) {
        String flag = itemServiceDao.deleteItem((long) Integer.parseInt(itemId.get("itemId")));
        if (flag == null) {
            // 静态方法可以直接调用
            return Msg.fail();
        }
        return Msg.success();
    }


    @ApiOperation("删除物料信息")
    /**
     * 使用delete会产生bad request。具体咱也不清楚
     */
    @RequestMapping(value = "/batchDelItem", method = RequestMethod.POST)
    @ResponseBody
    public Msg batchDelItem(@RequestBody Map<String, Object> itemId) {
        List idsList = (List) itemId.get("idsList");
        int count = itemServiceDao.batchDelItem(idsList);
        if (count==0){
            return Msg.fail();
        }
        return Msg.success();
    }

    @ApiOperation("根据id查询物料信息")
    @RequestMapping(value = "/selectItemById", method = RequestMethod.GET)
    @ResponseBody
    public Msg selectItemById(@RequestParam("itemId") Long itemId) {
        Item item = itemServiceDao.selectItemById(itemId);
        return Msg.success().add("item", item);
    }
}
